package com.bwie.system.consumer;

import com.bwie.system.service.ShopService;
import com.rabbitmq.client.Channel;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Author：作者姓名
 * @Package：com.bwie.system.consumer
 * @Project：_yk
 * @name：Consumer
 * @Date：2024/6/17 20:28
 */
@Component
@Log4j2
public class Consumer {

    @Autowired
    private ShopService shopService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @RabbitListener(queuesToDeclare = @Queue("pxin"))
    public void Shop(Message message, Channel channel){
        String messageId = message.getMessageProperties().getMessageId();
        try {
            Long login = redisTemplate.opsForSet().add("admin", messageId);
            if(login==1){
                System.out.println("申请已通过，退款成功");
            }
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            try {
                redisTemplate.opsForSet().remove("admin");
                channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }
    }


}
